<template>
  <div>
    <el-menu class="el-menu-demo">
      <el-menu-item index="1">
        <h2>修改密码</h2>
      </el-menu-item>

      <el-divider style="margin: 0"></el-divider>
    </el-menu>
    <el-form label-width="100px" class="demo-ruleForm" :rules="rules">
      <el-form-item label="当前密码" prop="oldpwd" >
        <el-input
          type="password"
          placeholder="当前登录密码"
          style="width: 300px"
          v-model="form.oldpwd"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newpwd">
        <el-input
          type="password"
          placeholder="6-20位数字、字母、下划线"
          style="width: 300px"
          v-model="form.newpwd"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="newpwd1">
        <el-input  type="password" placeholder="请再次输入密码" style="width: 300px" v-model="form.newpwd1"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width: 200px; border-radius: 20px"
        @click="submitForm('form')">保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
  
  <script>
export default {
  data() {
    // var validatePass = (rule, value, callback) => {
    //     if (value === '') {
    //       callback(new Error('请输入密码'));
    //     } else {
    //       if (this.form.newpwd1 !== '') {
    //         this.$refs.form.validateField('newpwd1');
    //       }
    //       callback();
    //     }
    //   };
    //   var validatePass2 = (rule, value, callback) => {
    //     if (value === '') {
    //       callback(new Error('请再次输入密码'));
    //     } else if (value !== this.form.newpwd) {
    //       callback(new Error('两次输入密码不一致!'));
    //     } else {
    //       callback();
    //     }
    //   };
    return {
      form: {
        oldpwd: "",
        newpwd: "",
        newpwd1: "",
      },
      rules: {
          oldpwd: [
          { required: true, message: "请填写当前密码", trigger: "blur" },
            
          ],
          newpwd: [
            { required: true, message: "请填写新密码", trigger: "blur" },
            {
              pattern: /^\w{6,15}$/,
              message: "6~15位字符，可以包含：数字、字母、下划线",
              trigger: "blur",
            },
          ],
          newpwd1: [
          { required: true, message: "再次输入新密码", trigger: "blur" },
           
          ],
        }
    };
  },
  methods: {
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let pwd	=this.form.newpwd
            
            console.log(pwd)
            var url = 'http://127.0.0.1:3000/user/xgmm'
            axios.post(url,{pwd}).then(res=>{
              alert('success')
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
  },
};
</script>
  
  <style lang="scss" scoped>
.demo-ruleForm {
  margin-left: 20px;
  background-color: #fff;
  padding: 10px 0;
}
.el-menu-demo {
  margin-left: 20px;
  border-radius: 10px;
  padding: 20px 0;
}
.el-divider {
  margin: 0;
}
</style>